package com.example.backgroundtask;

public class DaemonTest {


    public static void main(String[] args) {

        Thread daemon = new Thread(new DaemonRunnable(),"daemon");
        daemon.setDaemon(true);
        Thread task = new Thread(new TaskRunnable(),"task");

        daemon.start();
        task.start();

    }

    static class DaemonRunnable implements Runnable{

        @Override
        public void run() {
            test();
        }

        private  void test() {
            for (int i = 0; i < 10000; i++) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+":"+i);
            }
        }
    }

    static class TaskRunnable implements Runnable{


        @Override
        public void run() {
            test();
        }

        private  void test() {
            for (int i = 0; i < 1000; i++) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+":"+i);
            }
        }
    }


}
